/*
 * @Author: Wangjun
 * @Date: 2021-07-06 17:11:39
 * @LastEditTime: 2023-12-02 09:31:09
 * @LastEditors: wangjun haodreams@163.com
 * @Description:
 * @FilePath: \xr_node_calc\routers\router.go
 * hnxr
 */
package routers

import (
	"net"

	"gitee.com/haodreams/golib/autoroute"
	"gitee.com/haodreams/golib/autoroute/defaulter"
	"gitee.com/haodreams/golib/logs"
	"gitee.com/haodreams/libs/config"
	"gitee.com/haodreams/libs/routine"
	"gitee.com/haodreams/op_node/controllers"
	"github.com/gin-gonic/gin"
)

func Setup(listener net.Listener) (err error) {
	router := defaulter.DefaultEngine(func() (string, string) {
		return "magus", "taoge"
	}, config.DefaultString("gin.mode", gin.ReleaseMode))

	//node *path 前的路径需要和Controller前的类保持一致，并小写
	//autoroute.Normal(router, "/v1/log/*path", &defaulter.LogController{})
	autoroute.Normal(router, "/v1/system/*path", &defaulter.SystemController{})
	autoroute.Normal(router, "/v1/api/*path", &controllers.ApiController{})
	autoroute.Normal(router, "/v1/table/*path", &controllers.TableController{})

	go func() {
		logs.Infof("Listening and serving HTTPS on listener what's bind with address@%s", listener.Addr())
		err := autoroute.RunListener(listener, router, "server.pem", "server-key.pem")
		if err != nil {
			logs.Error(err)
		}
	}()

	//程序退出时自动关闭端口
	routine.AddFuncAfterStop(func() {
		listener.Close()
	})
	return
}
